#include <REGX52.h>
#include "Timer.h"
#include "Delay.h"

//蜂鸣器I/O口
sbit Buzzer_BZ = P2^5;
//播放速度，值为四分音符的时长(ms)
#define SPEED 500

//音阶和频率对照表的索引
#define P  	0
#define L1 	1
#define L1_ 2
#define L2  3
#define L2_ 4
#define L3  5
#define L4  6
#define L4_ 7
#define L5  8
#define L5_ 9
#define L6  10
#define L6_ 11
#define L7  12
#define M1 	13
#define M1_ 14
#define M2  15
#define M2_ 16
#define M3  17
#define M4  18
#define M4_ 19
#define M5  20
#define M5_ 21
#define M6  22
#define M6_ 23
#define M7  24
#define H1 	25
#define H1_ 26
#define H2  27
#define H2_ 28
#define H3  29
#define H4  30
#define H4_ 31
#define H5  32
#define H5_ 33
#define H6  34
#define H6_ 35
#define H7  36

//12个为一组，Frequency有小字组，小字1组，小字2组，相邻组从左到右高8度
//再低音为大字组，大字1组
//数组第一个为休止符
//频率对照表
unsigned int code Frequency[] = {
	0,
	63628, 63731, 63835, 63928, 64021, 64103, 64185, 64260, 64331, 64400, 64463, 64524,	
	64580, 64633, 64684, 64732, 64777, 64820, 64860, 64898, 64934, 64968, 65000, 65030, 
	65058, 65085, 65110, 65134, 65157, 65178, 65198, 65217, 65235, 65252, 65268, 65283
};

////小星星乐谱
////两个一组：频率索引，播放时长
//unsigned char code Music[] = {
//	//S1
//	M1, 4,
//	M1, 4,
//	M5, 4,
//	M5, 4,
//	//S2
//	M6, 4,
//	M6, 4,
//	M5, 4+4,
//	//S3
//	M4, 4,
//	M4, 4,
//	M3, 4,
//	M3, 4,
//	//S4
//	M2, 4,
//	M2, 4,
//	M1, 4+4,
//	//S5
//	M5, 4,
//	M5, 4,
//	M4, 4,
//	M4, 4,
//	//S6
//	M3, 4,
//	M3, 4,
//	M2, 4+4,
//	//S7
//	M5, 4,
//	M5, 4,
//	M4, 4,
//	M4, 4,
//	//S8
//	M3, 4,
//	M3, 4,
//	M2, 4+4,
//	//S9
//	M1, 4,
//	M1, 4,
//	M5, 4,
//	M5, 4,
//	//S10
//	M6, 4,
//	M6, 4,
//	M5, 4+4,
//	//S11
//	M4, 4,
//	M4, 4,
//	M3, 4,
//	M3, 4,
//	//S12
//	M2, 4,
//	M2, 4,
//	M1, 4+4,
//	//终止符
//	0xFF
//};

//天空之城乐谱
unsigned char code Music[] = {
	//S1
	P,4,
	P,4,
	P,4,
	M6,2,
	M7,2,
	//S2
	H1,4+2,
	M7,2,
	H1,4,
	H3,4,
	//S3
	M7,4+4+4,
	M3,2,
	M3,2,
	//S4
	M6,4+2,
	M5,2,
	M6,4,
	H1,4,
	//S5
	M5,4+4+4,
	M3,4,
	//S6
	M4,4+2,
	M3,2,
	M4,4,
	H1,4,
	//S7
	M3,4+4,
	P,2,
	H1,2,
	H1,2,
	H1,2,
	//S8
	M7,4+2,
	M4_,2,
	M4,4,
	M7,4,
	//S9
	M7,4+4,
	P,4,
	M6,2,
	M7,2,
	//S10
	H1,4+2,
	M7,2,
	H1,4,
	H3,4,
	//S11
	M7,4+4+4,
	M3,2,
	M3,2,
	//S12
	M6,4+2,
	M5,2,
	M6,4,
	H1,4,
	//S13
	M5,4+4+4,
	M2,2,
	M3,2,
	//S14
	M4,4,
	H1,2,
	M7,2+2,
	H1,2+4,
	//S15
	H2,2,
	H2,2,
	H3,2,
	H1,2+4+4,
	//S16
	H1,2,
	M7,2,
	M6,2,
	M6,2,
	M7,4,
	M5_, 4,
	//S17
	M6,4+4+4,
	H1,2,
	H2,2,
	//S18
	H3,4+2,
	H2,2,
	H3,4,
	H5,4,
	//S19
	H2,4+4+4,
	M5,2,
	M5,2,
	//S20
	H1,4+2,
	M7,2,
	H1,4,
	H3,4,
	//S21
	H3,4+4+4+4,
	//S22
	M6,2,
	M7,2,
	H1,4,
	M7,4,
	H2,2,
	H2,2,
	//S23
	H1,4+2,
	M5,2+4+4,
	//S24
	H4,4,
	H3,4,
	H2,4,
	H1,4,
	//S25
	H3,4+4+4,
	H3,4,
	//S26
	H6,4+4,
	H5,4,
	H5,4,
	//S27
	H3,2,
	H2,2,
	H1,4+4,
	P,2,
	H1,2,
	//S28
	H2,4,
	H1,2,
	H2,2,
	H2,4,
	H5,4,
	//S29
	H3,4+4+4,
	H3,4,
	//S30
	H6,4+4,
	H5,4+4,
	//S31
	H3,2,
	H2,2,
	H1,4+4,
	P,2,
	H1,2,
	//S32
	H2,4,
	H1,2,
	H2,2+4,
	M7,4,
	//S33
	M6,4+4+4,
	P,4,
	0xFF//终止标志
};


//				控制频率  		遍历乐谱
unsigned char FrequencySelect, MusicSelect;

void main()
{
	Timer0_Init();
    while(1)
    {
		//终止符:音乐结束
        if(Music[MusicSelect] != 0xFF)
		{
			FrequencySelect = Music[MusicSelect];//选择音符对应频率
			MusicSelect++;
			Delayms(SPEED / 4 * Music[MusicSelect]);//选择播放时长
			MusicSelect++;
			//相邻音符停顿一下下
			TR0 = 0;
			Delayms(5);
			TR0 = 1;
		}
		else//终止符，停止播放
		{
			TR0 = 0;//关闭定时器
			while(1);
		}
    }
}

//蜂鸣器按频率发声
void Timer0_Rountine(void) interrupt 1
{
	//如果不是休止符
	if(Frequency[FrequencySelect])
	{
		TH0 = Frequency[FrequencySelect] / 256;		//高8位
		TL0 = Frequency[FrequencySelect] % 256;		//低8位
		Buzzer_BZ = !Buzzer_BZ;

	}
}